<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="id" prop="id" sortable>
      </el-table-column>

      <el-table-column align="center" label="商品id" prop="goodId" sortable>
      </el-table-column>

      <el-table-column align="center" label="商品名称" prop="goodName" sortable>
      </el-table-column>

      <el-table-column align="center" label="拼团数量" prop="leastNum">
      </el-table-column>

      <el-table-column align="center" label="订单详情" prop="">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showOrder(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="剩余数量" prop="orderLeftNum">
      </el-table-column>

      <el-table-column align="center" label="启用状态" prop="deleted">
        <template slot-scope="scope">
          <el-tag :type="scope.row.deleted ? 'true' : 'false' ">{{ scope.row.deleted ? '已停用' : '已启用' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">停用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 订单详情 -->
    <el-dialog title="订单详情" width="800" :visible.sync="orderVisible">
      <el-form :data="orderUserForm" label-position="left">
        <el-form-item>
          <el-table size="small" :data="orderUserForm" border fit highlight-current-row>
            <el-table-column align="center" label="用户id" prop="userId" />
            <el-table-column align="center" label="订单id" prop="orderId" />
            <el-table-column align="center" label="商品名称" prop="goodName" />
            <el-table-column align="center" label="订单数量" prop="orderNum" />
            <el-table-column align="center" label="订单状态" prop="orderStatus">
              <template slot-scope="scope">
                <el-tag>{{handleType[scope.row.orderStatus]}}</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="商品数量修改" :visible.sync="planFormVisible">
      <el-form ref="planForm" :model="planForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>

        <el-form-item label="奖品数量" prop="leastNum">
          <el-input-number v-model="planForm.leastNum" :min="0"></el-input-number>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="planFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updatePlan">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 170px;
  display: block;
}
.avatars {
  width: 150px;
  height: 170px;
  display: block;
}
</style>

<script>
import { getDetail, deleteDetail, updateDetail } from '@/api/group'
import { getUserOrder } from '@/api/groupUser'
import { getToken } from '@/utils/auth'

export default {
  name: 'Ad',
  computed: {
    headers() {
      return {
        'X-Token': getToken()
      }
    }
  },
  data() {
    return {
      handleType: ['', '未发货', '已发货', '已退款'],
      list: undefined,
      total: undefined,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        awardId: undefined,
        sort: '+id'
      },
      planFormVisible: false,
      orderVisible: false,
      dialogFormVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      groupId: undefined,
      planForm: {
        id: undefined,
        leastNum: undefined
      },
      orderUserForm: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      this.listQuery.id = this.$route.params.groupId
      this.getList()
    },

    getList() {
      this.listLoading = true
      getDetail(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    showOrder(id) {
      getUserOrder({ groupId: id }).then(response => {
        this.orderUserForm = response.data.data
      })
      this.orderVisible = true
    },
    handleUpdate(row) {
      this.planForm.id = row.id
      this.planForm.leastNum = row.leastNum
      this.groupId = row.groupId
      this.planFormVisible = true
    },
    updatePlan() {
      updateDetail({ id: this.planForm.id, num: this.planForm.leastNum, groupId: this.groupId })
        .then(() => {
          for (const v of this.list) {
            if (v.id === this.planForm.id) {
              const index = this.list.indexOf(v)
              this.list.splice(index, 1, this.dataForm)
              break
            }
          }
          this.planFormVisible = false
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
        .catch(res => {
          this.$notify({
            title: '失败',
            message: res.data.errmsg,
            type: 'error',
            duration: 6000
          })
        })
    },
    handleDelete(row) {
      this.$confirm('此操作会停用活动安排, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDetail({ id: row.id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    }
  }
}
</script>
